package utils

import (
	"github.com/pkg/errors"
	"os"
	"syscall"
)

func GetFstatInfo() (*FstatInfo, error) {
	wd, err := os.Getwd()
	if err != nil {
		return nil, errors.Wrap(err, "Get cwd failed")
	}

	var stat syscall.Statfs_t
	err = syscall.Statfs(wd, &stat)
	if err != nil {
		return nil, errors.Wrap(err, "syscall failed")
	}

	var out = &FstatInfo{
		Total: int(stat.Blocks * uint64(stat.Bsize)),
		Avail: int(stat.Bfree * uint64(stat.Bsize)),
	}
	out.Used = out.Total - out.Avail
	return out, nil
}
